fix: add missing fflush(stdout) to write log message immediately#442
fix: add missing fflush(stdout) to write log message immediately#442stackia merged 1 commit intostackia:mainfrom
Conversation
Without fflush(stdout), log messages have incorrect timestamp (added by syslogd or journald), or even get printed out when exit() but not startup.
|
谢谢,确实是个 bug |
There was a problem hiding this comment.
Pull request overview
This PR aims to make stdout-based logging appear immediately (so external log collectors don’t assign misleading timestamps) by flushing stdout in the logger() helper.
Changes:
- Flush
stdoutafter writing a log line to ensure it is emitted immediately. - Adjust
logger()’s doc comment and attempt to account for the auto-appended newline in the return value.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * @param level Message log level | ||
| * @param format printf style format string | ||
| * @returns Whatever printf returns | ||
| * @return Message length (newline included) |
There was a problem hiding this comment.
The updated docstring claims this function returns the message length (newline included), but the implementation still returns fputs()'s status code (and potentially modified by later logic). Either adjust the implementation to return the actual number of characters written (based on the rendered message and whether a newline was appended), or keep the original semantics and document it as a fputs()-style return (non-negative on success, EOF on error).
| * @return Message length (newline included) | |
| * @return Non-negative on success, EOF on output error |
| fputc('\n', stdout); | ||
| r++; |
There was a problem hiding this comment.
r++ here assumes r is a character count, but r is currently the return value from fputs() (a non-negative/EOF status). Incrementing it can also mask an EOF from fputs() (e.g., -1 becomes 0), changing error signaling. Suggestion: remove the r++ unless you change r to track bytes written, and if you do, ensure errors from fputs()/fputc() aren’t lost.
| fputc('\n', stdout); | |
| r++; | |
| if (fputc('\n', stdout) == EOF) { | |
| r = EOF; | |
| } |
…ely (stackia#442)" This reverts commit 201a12c.
Without fflush(stdout), log messages have incorrect timestamp (added by syslogd or journald), or even get printed out when exit() but not startup.